const express = require('express');

// 实体类
const ObjId = require('../entity/common/ObjId');
const Response = require('../entity/http/Response');
const UpdateWashRecord = require('../entity/wash/UpdateWashRecord');

// 业务层
const washService = require('../service/wash');

const router = express.Router();

/**
 * @apiName 获取所有洗消记录接口
 */
router.get('/washRecordList', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 GET /wash/washRecordList`);
    const { uid } = req.token_payload.data;
    washService.getAllWashRecord(req.requestId, uid).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 获取所有洗消记录响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 获取所有洗消记录时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 查看洗消记录详情接口
 */
router.get('/wash-record', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 GET /wash/wash-record`);
    const { uid } = req.token_payload.data;
    let washRecordId;
    try {
        washRecordId = new ObjId(req.query.id).id;
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "洗消记录Id有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 洗消记录Id ${ JSON.stringify(washRecordId) }`);
    washService.getWashRecordInfo(req.requestId, uid, washRecordId).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 获取洗消记录响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 获取洗消记录时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 修改洗消记录接口
 * @param { number } id                    洗消记录ID
 * @param { string } doctorName            医生姓名
 * @param { string } patientName           患者姓名
 * @param { string } patientSex            患者性别
 * @param { string } patientID             患者身份证号
 * @param { string } diagnosisNumber       诊号
 * @param { string } hospitalizationNumber 住院号
 * @param { string } remark                备注信息
 */
router.put('/wash-record', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 PUT /wash/wash-record`);
    const { uid } = req.token_payload.data;
    let washRecord;
    try {
        washRecord = new UpdateWashRecord(req.body);
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "洗消记录信息有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 洗消记录 ${ JSON.stringify(washRecord) }`);
    washService.updateWashRecord(req.requestId, uid, washRecord).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 修改洗消记录响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 修改洗消记录时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 删除洗消记录接口
 * @param { number } id     洗消记录ID
 */
router.delete('/wash-record', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 DELETE /wash/wash-record`);
    const { uid } = req.token_payload.data;
    let washRecordId;
    try {
        washRecordId = new ObjId(req.query.id).id;
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "洗消记录Id有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 洗消记录Id ${ JSON.stringify(washRecordId) }`);
    washService.delWashRecordInfo(req.requestId, uid, washRecordId).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 删除洗消记录响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 删除洗消记录时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

module.exports = router;